package com.java1234.jxc.controller.admin;

import com.java1234.jxc.beans.Goods;
import com.java1234.jxc.core.page.Pagination;
import com.java1234.jxc.service.GoodsService;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@SuppressWarnings("all")
@RestController
@RequestMapping("/admin/goods")
public class GoodsController {

    @Autowired
    Dao dao;
    @Autowired
    GoodsService goodsService;

    @RequestMapping("/deleteStock")
    public Object deleteStock(Integer id) throws Exception{
        NutMap result = NutMap.NEW();
        Goods goods = goodsService.fetch(id);
        if (goods.getState()==2){
            result.put("success", false);
            result.put("errorInfo", "该商品已经发生单据，不能删除");
        }else{
            goods.setInventoryQuantity(0);
            goodsService.updateIgnoreNull(goods);
            result.put("success", true);
        }
       return result;
    }


    /**
     * 添加商品到仓库 修改库存以及价格信息
     * @param id
     * @param num
     * @param price
     * @return
     * @throws Exception
     */
    @RequestMapping("/saveStore")
    public Object saveStore(Integer id,Integer num,float price) throws  Exception{
        NutMap result = NutMap.NEW();
        Goods goods = goodsService.fetch(id);
        goods.setInventoryQuantity(num);
        goods.setPurchasingPrice(price);
        goods.setLastPurchasingPrice(price);
        goodsService.updateIgnoreNull(goods);

        result.put("success",true);
        return result;
    }



    /**
     * 根据条件分页查询有库存的商品信息
     * @param codeOrName
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/listHasInventoryQuantity")
    public Object listHasInventoryQuantity(String codeOrName,
                                          @RequestParam(value = "page",required = false,defaultValue = "1")Integer page,
                                          @RequestParam(value = "rows",required = false,defaultValue = "10")Integer rows
    ){
        NutMap result = NutMap.NEW();

        Cnd cnd = Cnd.where(null);
        cnd.and("inventoryQuantity",">",0);
        if (Strings.isNotBlank(codeOrName)){

            cnd.and("code","like","%"+codeOrName+"%").or("name","like","%"+codeOrName+"%");
        }
        Pagination pagination = goodsService.listPage(page, rows, cnd);
        List<?> list = pagination.getList();
        list.forEach(item->{
            dao.fetchLinks(item,null);
        });
        long total = pagination.getTotalCount();
        result.put("rows",list);
        result.put("total",total);
        return result;
    }

    /**
     * 根据条件分页查询没有库存的商品信息
     * @param codeOrName
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/listNoInventoryQuantity")
    public Object listNoInventoryQuantity(String codeOrName,
                                          @RequestParam(value = "page",required = false,defaultValue = "1")Integer page,
                                          @RequestParam(value = "rows",required = false,defaultValue = "10")Integer rows
                                          ){
        NutMap result = NutMap.NEW();

        Cnd cnd = Cnd.where(null);
        cnd.and("inventoryQuantity","=",0);
        if (Strings.isNotBlank(codeOrName)){

            cnd.and("code","like","%"+codeOrName+"%").or("name","like","%"+codeOrName+"%");
        }
        Pagination pagination = goodsService.listPage(page, rows, cnd);
        List<?> list = pagination.getList();
        list.forEach(item->{
            dao.fetchLinks(item,null);
        });
        long total = pagination.getTotalCount();
        result.put("rows",list);
        result.put("total",total);
        return result;
    }



    /**
     * 分页查询商品信息
     * @param goods
     * @param page
     * @param rows
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    public Object list(Goods goods,
                       @RequestParam(value = "page",required = false,defaultValue = "1")Integer page,
                       @RequestParam(value = "rows",required = false,defaultValue = "10")Integer rows)throws Exception{
        NutMap result = NutMap.NEW();

        String name = goods.getName();
        Integer typeId = goods.getTypeId();
        if (typeId==null)typeId=1;
        Cnd cnd = Cnd.where(null);

        System.out.println("typeId:::::"+typeId);

        if (Strings.isNotBlank(name)){
            cnd.and("name","like","%"+name+"%");
        }
        if (typeId!=1){
            cnd.and("typeId","=",typeId);
        }

        Pagination pagination = goodsService.listPage(page, rows, cnd);
        List<?> list = pagination.getList();
        list.forEach(item->{
            dao.fetchLinks(item,null);
        });
        int total = pagination.getTotalCount();

        result.put("rows", list);
        result.put("total", total);

        return result;
    }

    /**
     * 生成商品编码
     * @return
     * @throws Exception
     */
    @RequestMapping("/genGoodsCode")
    public String genGoodsCode()throws Exception{
        int code = dao.func(Goods.class, "max", "code");
        if (code!=0){
            ++code;
            String codes = code+"";
            int length = codes.length();
            for(int i =4;i>length;i--){
                codes="0"+codes;
            }
            return codes;

        }else{
            return "0001";
        }
    }

    /**
     * 添加或修改商品信息
     * @param goods
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    public Object save(Goods goods) throws Exception{
        NutMap result = NutMap.NEW();
        System.out.println("goods.getTypeId()>>>>"+goods.getTypeId());
        if (goods.getId()==null){
        // 新增
            goods.setLastPurchasingPrice(goods.getPurchasingPrice());
            goodsService.insert(goods);
        }else{
            goodsService.updateIgnoreNull(goods);
        }
        result.put("success",true);
        return result;
    }

    @RequestMapping("/delete")
    public Object delete(Integer id) throws Exception{
        NutMap result = NutMap.NEW();
        Goods goods = goodsService.fetch(id);

        if(goods.getState()==1){
            result.put("success", false);
            result.put("errorInfo", "该商品已经期初入库，不能删除");
        }else if(goods.getState()==2){
            result.put("success", false);
            result.put("errorInfo", "该商品已经发生单据，不能删除");
        }else{
            goodsService.delete(id);
            result.put("success", true);
        }
        return result;

    }



}
